package com.rec.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rec.entity.Memberships;
import com.rec.repository.MembershipsRepository;
import com.rec.service.MembershipsService;
@Service
@Transactional
public class MembershipsServiceImpl implements MembershipsService {
	@Autowired
	private MembershipsRepository r;
	@Override
	public Map<String, Object> getAll() {
		Map<String ,Object> data = new HashMap<String, Object>();
		data.put("data", r.findAll());
		return data;
	}
	@Override
	public Map<String, Object> getSearch(Map<String, Object> params) {
		Specification<Memberships> spec = getSpec(params);
		// 创建分页对象
		Integer page = (Integer) params.get("page");
		Integer limit = (Integer) params.get("limit");
		Pageable pa = PageRequest.of(page, limit);
		Page<Memberships> result = r.findAll(spec,pa);
		List<Memberships> data = result.getContent();
		long count = result.getTotalElements();
		if(data.size()>0) {
			params.put("code", 0);
			params.put("data", data);
			params.put("count", count);
			
		} else {
			params.put("code", 1);
			params.put("msg", "没有数据！");
		}
		return params;
	}
	private Specification<Memberships> getSpec(Map<String, Object> params) {
		return new Specification<Memberships>() {

			@Override
			public Predicate toPredicate(Root<Memberships> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate pre = cb.conjunction();
				List<Expression<Boolean>> sqlList = pre.getExpressions();
				
				if(!StringUtils.isEmpty(params.get("department"))) {
					sqlList.add(cb.like(root.get("department"),"%"+params.get("department")+"%"));
				}
				if(!StringUtils.isEmpty(params.get("specialty"))) {
					sqlList.add(cb.like(root.get("specialty"),"%"+params.get("specialty")+"%"));
				}
				if(!StringUtils.isEmpty(params.get("degree"))) {
					sqlList.add(cb.equal(root.get("degree"),params.get("degree")));
				}
				return pre;
			}
			
		};
	}
	@Override
	public Map<String, Object> addNew(Map<String, Object> params) {
		Memberships m = (Memberships) params.get("mem");
		Memberships b = r.save(m);
		if(b!=null) {
			params.put("code", 0);
			params.put("msg", "新增成功！");
		}else {
			params.put("code", 1);
			params.put("msg", "新增失败！");
		}
		return params;
	}
	@Override
	public Map<String, Object> updateM(Map<String, Object> params) {
		Integer id = (Integer) params.get("id");
		String department = (String) params.get("department");
		String specialty = (String) params.get("specialty");
		String degree = (String) params.get("degree");
		Integer n = r.updateMem(id,department,specialty,degree);
		if(n>0) {
			params.put("code", 0);
			params.put("msg", "修改成功！");
		}else {
			params.put("code", 1);
			params.put("msg", "修改失败！");
		}
	return params;
	}
	

}
